﻿using QG.JobBack.BLL.IService;
using QG.JobBack.Core;
using QG.JobBack.DAL;
using QH.JobBack.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QG.JobBack.BLL.Service
{
    public class FeedBackService : BaseRepository<Feedbackinfo>, IFeedBackService
    {
        /// <summary>
        /// 查询所有反馈信息（table显示）
        /// </summary>
        /// <returns></returns>
        public List<FeedbackBase> QueryFeedBackAll(string UserName,Nullable<DateTime> FeedTime, Nullable<bool> IsLook)
        {
            using (var db = new qgpingEntities())
            {
                var str = from a in db.Feedbackinfo
                          join b in db.UserInfo
                          on a.UserId equals b.UserId


                          select new FeedbackBase()
                          {
                              FId=a.FId,
                              UserName = b.UserName,
                              Content=a.Content,
                              FeedTime=a.FeedTime,
                              Status=a.STATUS,
                              IsLook=a.IsLook,
                          };
                var tolists= str.ToList();
                if (!string.IsNullOrEmpty(UserName))
                {
                    tolists = tolists.Where(a => a.UserName == UserName).ToList();
                }
                if (!string.IsNullOrEmpty(FeedTime.ToString()))
                {
                    tolists = tolists.Where(b => b.FeedTime.ToString("yyyy-MM-dd") == FeedTime.Value.ToString("yyyy-MM-dd")).ToList();
                }
                if (IsLook==true)
                {
                    tolists = tolists.Where(a => a.IsLook == true).ToList();
                }
                if (IsLook == false)
                {
                    tolists = tolists.Where(a => a.IsLook == false).ToList();
                }

                return tolists.ToList<FeedbackBase>();
            }
        }

       

        public bool ReplyFeedBackInfo(Reply reply,int FId)
        {
            //代码执行完，则释放资源   声明一个实例
            using (var db = new qgpingEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //添加回复表
                        db.Reply.Add(reply);
                        


                        db.SaveChanges();
                        tran.Commit();

                       

                        
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        return false;
                    }
                }
                using (var tran2 = db.Database.BeginTransaction())
                {
                    try
                    {
                        Feedbackinfo feedBackInfo = db.Feedbackinfo.Where(a => a.FId == FId).FirstOrDefault();
                        feedBackInfo.IsLook = true;

                        db.SaveChanges();
                        tran2.Commit();
                    }
                    catch (Exception)
                    {
                        tran2.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        
    }
}
